package com.dmx.cep.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.dmx.cep.dao.EpgDao;
import com.dmx.cep.entity.Channel;
import com.dmx.cep.entity.ChannelType;
import com.dmx.cep.entity.ConfAttributeSelection;
import com.dmx.cep.entity.TvContentInfo;
import com.dmx.cep.entity.TvContentStatus;
import com.dmx.cep.entity.TvContentType;
import com.dmx.cep.service.EpgService;
import com.dmx.fw.service.impl.BaseServiceImpl;

public class EpgServiceImpl extends BaseServiceImpl implements EpgService {

	private EpgDao epgDao;
	
	public List<ChannelType> getChannelTypeList(){
		return epgDao.getChannelTypeList();
	}

	public EpgDao getEpgDao() {
		return epgDao;
	}

	public void setEpgDao(EpgDao epgDao) {
		this.epgDao = epgDao;
	}

	@Override
	public boolean isChannelCodeExists(String code) throws Exception{
		// TODO Auto-generated method stub
		return epgDao.isChannelCodeExists(code);
	}

	@Override
	public boolean isChannelIdExists(Long channelId) throws Exception{
		// TODO Auto-generated method stub
		return epgDao.isChannelIdExists(channelId);
	}

	@Override
	public boolean isChannelNameExists(String name) throws Exception{
		// TODO Auto-generated method stub
		return epgDao.isChannelNameExists(name);
	}
	
	public TvContentType getContentTypeByName(String typeName) throws Exception{
		return epgDao.getContentTypeByName(typeName);
	}

	@Override
	public List<TvContentType> getContentTypeList() throws Exception {
		// TODO Auto-generated method stub
		return epgDao.getContentTypeList();
	}

	@Override
	public void removeEPGPrograms(String channelCode, Date epgStartTime)
			throws Exception {
		// TODO Auto-generated method stub
		epgDao.removeEPGPrograms(channelCode, epgStartTime);
	}

	@Override
	public Date getLastProgramEndTime(String channelCode) throws Exception {
		// TODO Auto-generated method stub
		return epgDao.getLastProgramEndTime(channelCode);
	}

	@Override
	public void batchOverWritePrograms(String channelCode, Date epgStartTime,
			List<TvContentInfo> tvContentList,
			List<TvContentStatus> tvContentStatusList) throws Exception {
		// TODO Auto-generated method stub
		log.info("batch update programme...");
		epgDao.removeEPGPrograms(channelCode, epgStartTime);
		
		Date lastProgramEndTime=epgDao.getLastProgramEndTime(channelCode);
		
		log.info("lastProgramEndTime="+lastProgramEndTime);
		if(lastProgramEndTime!=null){
			List<TvContentInfo> invalidContentList=new ArrayList<TvContentInfo>();
			for(TvContentInfo content:tvContentList){
				if(content.getStartTime().before(lastProgramEndTime)){
					invalidContentList.add(content);
				}
			}
			tvContentList.removeAll(invalidContentList);
			
			List<TvContentStatus> invalidContenStatusList=new ArrayList<TvContentStatus>();
			for(TvContentStatus contentStatus:tvContentStatusList){
				for(TvContentInfo content: invalidContentList){
					if(contentStatus.getContentId().equals(content.getContentId()))
						invalidContenStatusList.add(contentStatus);
				}
			}
			tvContentStatusList.removeAll(invalidContenStatusList);
		}
		
		log.info("tvContentList.size="+tvContentList.size());
		log.info("tvContentStatusList.size="+tvContentStatusList.size());
		
		this.saveAll(tvContentList);
		
		this.saveAll(tvContentStatusList);
	}

	@Override
	public List<Channel> getChannelList() {
		// TODO Auto-generated method stub
		return epgDao.getChannelList();
	}

	@Override
	public boolean isChannelExists(String channelCode) throws Exception {
		// TODO Auto-generated method stub
		return epgDao.isChannelExists(channelCode);
	}

	@Override
	public Integer deleteExpiredEpgData(Date validDate, int maxResults)
			throws Exception {
		// TODO Auto-generated method stub
		return epgDao.deleteExpiredEpgData(validDate, maxResults);
	}

	@Override
	public ConfAttributeSelection getConfAttributeByKey(String key)
			throws Exception {
		// TODO Auto-generated method stub
		return epgDao.getConfAttributeByKey(key);
	}

	@Override
	public List<TvContentInfo> getTvContentListByChannelCode(String channelCode)
			throws Exception {
		// TODO Auto-generated method stub
		return epgDao.getTvContentListByChannelCode(channelCode);
	}
}
